﻿using System;
using System.Collections.Generic;
using System.Text;

namespace ME860.DotNetty.Common
{
    /// <summary>
    /// 控制台帮助类
    /// </summary>
    public static class ConsoleHelper
    {
        /// <summary>
        /// 写入控制台
        /// </summary>
        /// <param name="title">标题</param>
        /// <param name="message">消息</param>
        /// <param name="subTitle">副标题</param>
        /// <param name="consoleColor">控制台颜色</param>
        public static void Write(string title, string message, string subTitle, ConsoleColor consoleColor)
        {
            Console.ForegroundColor = consoleColor;
            var dateNowTxt = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss");
            Console.Write(string.IsNullOrEmpty(subTitle)
                ? $"[{dateNowTxt}]{title}:{message}"
                : $"[{dateNowTxt}]{title}[{subTitle}]:{message}");
            Console.ForegroundColor = ConsoleColor.White;
        }



        /// <summary>
        /// 写入控制台
        /// </summary>
        /// <param name="title">标题</param>
        /// <param name="message">消息</param>
        /// <param name="subTitle">副标题</param>
        /// <param name="consoleColor">控制台颜色</param>
        public static void WriteLine(string title, string message, string subTitle, ConsoleColor consoleColor)
        {
            Console.ForegroundColor = consoleColor;
            var dateNowTxt = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss");
            Console.WriteLine(string.IsNullOrEmpty(subTitle)
                ? $"[{dateNowTxt}]{title}:{message}"
                : $"[{dateNowTxt}]{title}[{subTitle}]:{message}");
            Console.ForegroundColor = ConsoleColor.White;
        }

        /// <summary>
        /// 服务写入控制台
        /// </summary>
        /// <param name="message">消息</param>
        /// <param name="subTitle">副标题</param>
        /// <param name="consoleColor">颜色</param>
        public static void ServerWrite(string message, string subTitle = null, ConsoleColor consoleColor = ConsoleColor.White)
        {
            Write("Server", message, subTitle, consoleColor);
        }

        /// <summary>
        /// 服务写入控制台
        /// </summary>
        /// <param name="message">消息</param>
        /// <param name="subTitle">副标题</param>
        /// <param name="consoleColor">颜色</param>
        public static void ServerWriteLine(string message, string subTitle = null, ConsoleColor consoleColor = ConsoleColor.White)
        {
            WriteLine("Server", message, subTitle, consoleColor);
        }
        /// <summary>
        /// 写入错误
        /// </summary>
        /// <param name="exception">异常</param>
        public static void ServerWriteError(Exception exception)
        {
            ServerWriteLine(GetErrorMessage(exception), "错误", ConsoleColor.Red);
        }


        #region 私有方法

        /// <summary>
        /// 获得错误消息
        /// </summary>
        /// <param name="exception"></param>
        /// <returns></returns>
        private static string GetErrorMessage(Exception exception)
        {
            var message = exception.Message;
            if (!string.IsNullOrEmpty(exception.StackTrace)) message += $"\r\n{exception.StackTrace}";

            if (exception is AggregateException aggregateException)
            {
                foreach (var innerException in aggregateException.InnerExceptions)
                {
                    var ex = innerException;
                    do
                    {
                        message += $"\r\n{GetErrorMessage(exception)}";
                        ex = innerException.InnerException;
                    } while (ex != null);
                }
            }
            else
            {
                while (exception.InnerException != null)
                {
                    exception = exception.InnerException;
                    message += $"\r\n{GetErrorMessage(exception)}";
                }
            }

            return message;
        }


        #endregion
    }
}
